package com.mz.ozy.mvp.presenter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.mz.ozy.base.BasePresenterImpl;
import com.mz.ozy.bean.FreightBean;
import com.mz.ozy.bean.OrderBean;
import com.mz.ozy.bean.VoucherBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.view.FirmOrderContract;
import com.mz.ozy.net.MyObserver;
import com.mz.ozy.net.RetrofitUtils;
import com.mz.ozy.utils.JsonUtils;
import com.mz.ozy.utils.MD5Util;
import com.mz.ozy.utils.RxUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.ResponseBody;

/**
 * Created by Administrator on 2018/12/7.
 */

public class FirmOrderPresenter extends BasePresenterImpl<FirmOrderContract.View> implements FirmOrderContract.Presenter {

    @Override
    public void submit(Activity activity, String content, String aid, String remark) {

        //                24、购物车下单（ShoppingCartOrderV1）
//                请求参数
//
//                参数名称	参数值	必须	说明
//                transCode		必须
//                t		必须	token
//                content	必须	购物车ID，多个ID以,号隔开
//                aid		必须	收货地址ID
//                remark			备注
//
//
//                返回
//                参数名称	参数值	必须	说明
//                orderNo			订单号

        Map<String, String> maps = new HashMap<>();
//        maps.put("transCode", Constants.TransCode.ShoppingCartOrderV1);
//        maps.put("t", SharePreferencesData.getSharePreferencesData(SBMallApp.ctx, Constants.PREE.TOKEN_ID));
//        maps.put("content", content);
//        maps.put("aid", aid);
//        maps.put("remark", "");
//        RetrofitUtils.getApiService(activity).post(maps)
//                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
//                .subscribe(new MyObserver<OrderBean>(mView, true) {
//
//                    @Override
//                    public void onSuccesss(OrderBean orderBean) {
//                        mView.onSubmitSuccess(orderBean);
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        super.onError(e);
//                        mView.onSubmitError("");
//                    }
//                });
    }

    @Override
    public void detailSubmit(Activity activity, String buylist, String gid, String aid, String freightId, String remark, String preType, String preActivId) {
        Map<String, String> maps = new HashMap<>();
        if (!TextUtils.isEmpty(buylist)) {
            maps.put("buyList", buylist);
        }
        maps.put("list", gid);
        maps.put("addressId", aid);
        maps.put("freightId", freightId);
        maps.put("remark", remark);
        maps.put("preType", preType);
        maps.put("preActivId", preActivId);
        RetrofitUtils.getApiService(activity).post(Constants.TransCode.TransCode10116, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<OrderBean>(mView, true) {

                    @Override
                    public void onSuccesss(OrderBean orderBean) {
                        mView.onSubmitSuccess(orderBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.onSubmitError(e.toString());
                    }
                });


    }

    @Override
    public void submitJf(Activity activity, String list, String addressId, String remark, String buyList) {

        Map<String, String> maps = new HashMap<>();
        if (!TextUtils.isEmpty(buyList)) {
            maps.put("buyList", buyList);
        }
        maps.put("list", list);
        maps.put("addressId", addressId);
        maps.put("remark", remark);
        RetrofitUtils.getApiService(activity).post(Constants.TransCode.TransCode10205, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<OrderBean>(mView, true) {

                    @Override
                    public void onSuccesss(OrderBean orderBean) {
                        mView.onSubmitJfSuccess(orderBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.onSubmitError(e.toString());
                    }
                });
    }

    public void onPay(Context contex, String orderId, int type, String pwd) {
        Map<String, String> maps = new HashMap<>();
        maps.put("id", orderId);
        maps.put("payType", String.valueOf(type));
        maps.put("payPwd", MD5Util.getMD5String(pwd));
        RetrofitUtils.getApiService(contex).post(Constants.TransCode.TransCode10117, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView, true) {

                    @Override
                    public void onSuccesss(JSONObject orderBean) {
                        mView.onPaySuccess(orderBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.onPayError("");
                    }
                }.setTransCode(Constants.TransCode.TransCode10117));
    }

    public void queryVoucher(Context context, String list) {
        Map<String, String> maps = new HashMap<>();
        maps.put("list", list);
        RetrofitUtils.getApiService(context).post(Constants.TransCode.TransCode10123, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<VoucherBean>(mView, true) {

                    @Override
                    public void onSuccesss(VoucherBean orderBean) {
                        mView.queryVoucherSuccess(orderBean);
                    }
                });
    }

    public void queryFreight(Context context, String activId, String list) {
        Map<String, String> maps = new HashMap<>();
        maps.put("preActivId", activId);
        maps.put("list", list);
        RetrofitUtils.getApiService(context).post(Constants.TransCode.TransCode10126, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<FreightBean>(mView, true) {

                    @Override
                    public void onSuccesss(FreightBean orderBean) {
                        mView.queryFreightSuccess(orderBean);
                    }
                });
    }
}
